package org.krishna.Dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.krishna.model.Category;
import org.krishna.model.UserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/*@Service("coreService")*/
@Service("saveData")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class SaveDataImpl implements SaveData{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SaveDataImpl.class);
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public Category getCategoryByName(String categoryName) {
		
		org.hibernate.Query query = this.sessionFactory.getCurrentSession().createQuery("from org.krishna.model.Category as c where c.categoryName = :categoryName");
		query.setParameter("categoryName", categoryName);
		@SuppressWarnings("unchecked")
		List<Category> list = query.list();
		if (list != null && list.size() >= 1) {
			LOGGER.info("size of Category is "+list.size());
			return (Category) list.get(0);
		}else{
			LOGGER.error("size of Category is "+list.size()+" so returning null");
			return null;
		}
	}

	@Override
	public boolean saveUserDetailsData(UserDetails userDetails) {
		boolean result = false;
		try{
			this.sessionFactory.getCurrentSession().save(userDetails);
			result = true;
			LOGGER.info("saved the details of the user for registring into database");
		}catch(HibernateException he){
			LOGGER.error("Error while saving the details of the user for registring into database");
			result = false;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserDetails getUserByUserName(String userName) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(UserDetails.class);
		criteria.add(Restrictions.eq("userName", userName));
		List<UserDetails> list = criteria.list();
		if (list != null && list.size() >= 1) {
			return (UserDetails) list.get(0);
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserDetails> getAllStudents() {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(UserDetails.class);
		Category category = this.getCategoryByName("student");
		criteria.add(Restrictions.eq("category", category));
		List<UserDetails> usersList = criteria.list();
		if (usersList != null && usersList.size() >= 1) {
			return  usersList;
		}else{
			return null;
		}
	}

	@Override
	public boolean updateUserDetailsData(UserDetails userDetails) {
		boolean result = false;
		try{
			this.sessionFactory.getCurrentSession().update(userDetails);
			result = true;
			LOGGER.info("Updated the details of the user into database");
		}catch(HibernateException he){
			LOGGER.error("Error while updating the details of the user into database");
			result = false;
		}
		return result;
	}
}
